package com.tenbit.tbw.ctl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tenbit.tbw.svc.BON1010SVC;
import com.tenbit.tbw.svc.EMP1010SVC;

@Controller
public class BON1010CTL {
	@Autowired
	private BON1010SVC svc;
	
	@Autowired
	private EMP1010SVC svc1;
	
	private Calendar calendar = Calendar.getInstance( );
	
	private Logger log=Logger.getLogger(this.getClass());
	
	@RequestMapping("BON1010M1.do")
	public ModelAndView getEmployeeBonusList(HttpServletRequest request)
	{
		log.debug(" ==============BON1010M1.do-getEmployeeBonusList: 상품상세정보 목록 ==============");
		ModelAndView mav = new ModelAndView();
		Map<String, Object> paramMap=new HashMap<String, Object>();
		Map<String, Object> resultMap=new HashMap<String, Object>();
		String departmentCode=request.getParameter("departmentCode");
		String bonusCode=request.getParameter("bonusCode");
		String bonusDate1=request.getParameter("bonusDate1");
		String bonusDate2=request.getParameter("bonusDate2");
		
		log.debug("첫 로딩1:"+departmentCode+bonusCode+bonusDate1+bonusDate2);
		
		if(departmentCode==null||departmentCode==("")){
			departmentCode="BUSINESS";
		}else if(departmentCode.equals("all")){
			departmentCode="all";
		}
		if(bonusCode==null||bonusCode==("")){
			bonusCode="B1";
		}else if(bonusCode.equals("all")){
			bonusCode="all";
		}
		if(bonusDate1==null||bonusDate1==("")){
			bonusDate1=calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+1;
		}
		if(bonusDate2==null||bonusDate2==("")){
			bonusDate2=calendar.get(Calendar.YEAR)+"-"+(calendar.get(Calendar.MONTH)+1)+"-"+calendar.get(Calendar.DAY_OF_MONTH);;
		}
		paramMap.put("departmentCode", departmentCode);
		paramMap.put("bonusCode", bonusCode);
		paramMap.put("bonusDate1", bonusDate1);
		paramMap.put("bonusDate2", bonusDate2);
		
		resultMap=svc.getEmployeeBonusInfoList(paramMap);
		
		
		paramMap.put("departmentCode", departmentCode);
		paramMap.put("bonusCode", bonusCode);
		paramMap.put("bonusDate1", bonusDate1);
		paramMap.put("bonusDate2", bonusDate2);
		String msg=(String) resultMap.get("msg");
		
		log.debug("첫 로딩2:"+paramMap);
		mav.addObject("model2",paramMap);
		mav.addObject("msg",msg);
		mav.addObject("model", svc1.getEmployeeCodebook());
		mav.setViewName("3rd_week/BON1010M1");
		log.debug(" ==============BON1010M1.do-getEmployeeBonusList: 종료 ==============");
		return mav;
		
	}
	@RequestMapping("BON1011M1.do")
	@ResponseBody
	public Map<String,Object> getEmployeeList(HttpServletRequest request){
		
		String departmentCode=request.getParameter("departmentCode");
		String bonusCode=request.getParameter("bonusCode");
		String bonusDate1=request.getParameter("bonusDate1");
		String bonusDate2=request.getParameter("bonusDate2");
		
		Map<String, Object> paramMap=new HashMap<String, Object>();
		
		paramMap.put("departmentCode", departmentCode);
		paramMap.put("bonusCode", bonusCode);
		paramMap.put("bonusDate1", bonusDate1);
		paramMap.put("bonusDate2", bonusDate2);
		
		Map<String,Object> resultMap= new HashMap<String,Object>();
		
		int page = 1;
		int limit=10;
		
		if(request.getParameter("page")!=null){
		
			page = Integer.parseInt(request.getParameter("page"));
		}
	
		
		resultMap=svc.getEmployeeInfoList(paramMap);
		
		List list=(List) resultMap.get("rows");
		
		int totalRecords=0;
		int totalpage=0;
		if(list!=null){
			
			totalRecords=list.size();
			totalpage=(int) (totalRecords/limit+1);
			}
		
		resultMap.put("page",page);
		resultMap.put("total",totalpage);
		resultMap.put("records",totalRecords);
		
		log.debug(resultMap);
		
		return resultMap;
		
	}
	
	@RequestMapping("BON1012M1.do")
	@ResponseBody
	public Map<String,Object> getBonusList(HttpServletRequest request){
		Map<String,Object> resultMap= new HashMap<String,Object>();
		
		String departmentCode=request.getParameter("departmentCode");
		String bonusCode=request.getParameter("bonusCode");
		String bonusDate1=request.getParameter("bonusDate1");
		String bonusDate2=request.getParameter("bonusDate2");
		
		
		Map<String, Object> paramMap=new HashMap<String, Object>();
		
		paramMap.put("departmentCode", departmentCode);
		paramMap.put("bonusCode", bonusCode);
		paramMap.put("bonusDate1", bonusDate1);
		paramMap.put("bonusDate2", bonusDate2);
		
		log.debug("BON1012M1++++++++++paramMap:"+paramMap);
		int page = 1;
		int limit=10;
	
		
		if(request.getParameter("page")!=null){
		
			page = Integer.parseInt(request.getParameter("page"));
		}

		
		
		resultMap=svc.getEmployeeBonusInfoList(paramMap);
		
		List list=(List) resultMap.get("rows");
		
		int totalRecords=0;
		int totalpage=0;
		if(list!=null){
			
			totalRecords=list.size();
			totalpage=(int) (totalRecords/limit+1);
			}
		
		resultMap.put("page",page);
		resultMap.put("total",totalpage);
		resultMap.put("records",totalRecords);
		log.debug(resultMap);
		
		return resultMap;
		
	}
	
	@RequestMapping("BON1013M1.do")
	@ResponseBody
	public Map<String,Object> setBonuslist(@RequestBody List<Map<String,Object> > list){
		
		Map<String,Object> resultMap= new HashMap<String,Object>();
		
		log.debug(list);
		resultMap=svc.setEmployeeBonusInfoList(list);
		
		
		resultMap.get("msg");
		
		log.debug(resultMap.get("msg"));
		return resultMap;
	}
}
